package zjhr.com.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zjhr.com.mapper.ZjUserMapper;
import zjhr.com.pojo.EasyUIPageQuery;
import zjhr.com.pojo.PageQuery;
import zjhr.com.pojo.ZjUser;
import zjhr.com.pojo.ZjUserExample;
import zjhr.com.service.UserService;
import zjhr.com.utils.IDUtils;
import zjhr.com.utils.TaotaoResult;
@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private ZjUserMapper ZjuserMapper;

	@Override
	public TaotaoResult save(ZjUser user) {
		if(StringUtils.isBlank(user.getFactoryId())){
			user.setFactoryId(null);
		}
		user.setId(IDUtils.genImageName());
		user.setCreatetime(new Date());
		ZjuserMapper.insert(user);
		return TaotaoResult.ok();
	}

	@Override
	public EasyUIPageQuery findListByPage(Integer page, Integer rows) {
		//PageQuery pageQuery = new PageQuery<ZjUser>();
		//totalpage
		ZjUserExample example = new ZjUserExample();
		 int total = ZjuserMapper.countByExample(example);
		//开始索引
		int start1 =(page-1)*rows;
		Integer start = new Integer(start1);
		List<ZjUser> query = ZjuserMapper.selectListByPageQuery(start,rows);
		EasyUIPageQuery pageQuery = new EasyUIPageQuery(total, query);
		return pageQuery;
	}
	//更改用户状态
	@Override
	public void checkUserType(String ids) {
		if(StringUtils.isNotBlank(ids)){
			String[] idStr = ids.split(",");
			for (String id : idStr) {
				ZjUser user = new ZjUser();
				user.setId(id);
				user.setUsertype("0");
				ZjuserMapper.updateByPrimaryKeySelective(user);
			}
			
		}
	}

}
